package com.ddx.myopengles20.newpackage;

import android.graphics.Color;
import android.opengl.GLES20;
import android.support.annotation.ColorInt;


import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;

//正多边形
public class Line {
    //顶点着色器(顶点画笔)
    public final String vertexShaderCode =
            "uniform mat4 uMVPMatrix;" +
                    "attribute vec4 vPosition;" +
                    "void main() {" +
                    "  gl_Position = uMVPMatrix * vPosition;" +
                    "}";
    //片着色器(内容填充色画笔)
    public final String fragmentShaderCode =
            "precision mediump float;" +
                    "uniform vec4 vColor;" +
                    "void main() {" +
                    "  gl_FragColor = vColor;" +
                    "}";

    private FloatBuffer vertexBuffer;//顶点位置数据
    float[] color = new float[4];//RGBA数据
    private static final int COORDS_PER_VERTEX = 3;//x,y,z三维坐标系
    private final int vertexStride = COORDS_PER_VERTEX * 4;
    private int positionHandle;//顶点画笔
    private int colorHandle;//片画笔
    private int vPMatrixHandle;//矩阵
    private float[] positions;//顶点数据

    public Line(float[] startPoint, float[] endPoint, @ColorInt int color) {
        this.color[0] = Color.red(color);
        this.color[1] = Color.green(color);
        this.color[2] = Color.blue(color);
        this.color[3] = Color.alpha(color);
        positions = createPositions(startPoint, endPoint);//获取顶点坐标
        ByteBuffer bb = ByteBuffer.allocateDirect(positions.length * 4);//预处理(格式化)顶点数据
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(positions);
        vertexBuffer.position(0);
    }

    public void init(int mProgram) {
        //获取对应画笔
        positionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
        colorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
        vPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    }

    ArrayList<Float> data = new ArrayList<>();

    private float[] createPositions(float[] startPoint, float[] endPoint) {
        data.clear();
        data.add(startPoint[0]);             //设置圆心坐标
        data.add(startPoint[1]);
        data.add(startPoint[2]);

        data.add(endPoint[0]);
        data.add(endPoint[1]);
        data.add(endPoint[2]);

        float[] f = new float[data.size()];
        for (int i = 0; i < f.length; i++) {
            f[i] = data.get(i);
        }
        return f;
    }

    // GL 支持画点  线  面(三角形)
    public void draw(int mProgram, float[] mvpMatrix) {
        int vertexCount = positions.length / COORDS_PER_VERTEX;
        GLES20.glUseProgram(mProgram);
        GLES20.glEnableVertexAttribArray(positionHandle);
        GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
                GLES20.GL_FLOAT, false,
                vertexStride, vertexBuffer);
        GLES20.glUniform4fv(colorHandle, 1, color, 0);
        GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, mvpMatrix, 0);
        //绘制连续线条
        GLES20.glLineWidth(50);
        GLES20.glDrawArrays(GLES20.GL_LINE_STRIP, 0, vertexCount);
        GLES20.glDisableVertexAttribArray(positionHandle);
    }

    //创建OpenGL绘制程序
    public int createProgram(String vertexSource, String fragmentSource) {
        int vertex = getShader(GLES20.GL_VERTEX_SHADER, vertexSource);
        if (vertex == 0) {
            return vertex;
        }
        int fragment = getShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
        if (fragment == 0) {
            return fragment;
        }
        int program = GLES20.glCreateProgram();//创建程序  必要
        if (program != 0) {
            GLES20.glAttachShader(program, vertex);//附加着色器 必要
            GLES20.glAttachShader(program, fragment);
            GLES20.glLinkProgram(program);//链接程序 必要
            int[] status = new int[1];
            GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, status, 0);
            if (status[0] == 0) {
                GLES20.glDeleteProgram(program);
            }
        }
        return program;
    }

    //获取着色器  编译多边形着色器代码
    public int getShader(int shaderType, String sourceCode) {
        int shader = GLES20.glCreateShader(shaderType); //创建着色器 必要
        if (shader != 0) {
            //着色器为已编译的二进制数据时可省以下步骤
            GLES20.glShaderSource(shader, sourceCode); //添加着色器代码 必要
            GLES20.glCompileShader(shader); //编译 必要
            int[] status = new int[1];
            GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, status, 0);
            if (status[0] == 0) {
                GLES20.glDeleteShader(shader);
                return 0;
            }
        }
        return shader;
    }

}

